package com.bjesc_b2c.app.appraiset.mvp.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseFragment;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.CarDetailsPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.CarDetailsPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CarDateilsView;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarDetailsBean;
import rxretrofit_base.model.KeyBean;

/**
 * Created by yidong on 2017/5/18.
 */

public class CarDetailsFragment extends BaseFragment implements CarDateilsView {
    private CarDetailsPresenter presenter;
    @InjectView(R.id.banner)
    Banner banner;
    @InjectView(R.id.tv_info_title)
    TextView titleTv;
    @InjectView(R.id.tv_info_status)
    TextView statusTv;
    @InjectView(R.id.tv_info_origin)
    TextView originTv;
    @InjectView(R.id.tv_info_price)
    TextView priceTv;
    @InjectView(R.id.tv_car_maintain)
    TextView carMaintainTv;
    @InjectView(R.id.tv_car_maintain_phone)
    TextView carMainPhoneTv;
    @InjectView(R.id.tv_car_dealers_name)
    TextView dealersNameTv;
    @InjectView(R.id.tv_dealer_contact_person)
    TextView dealersContactTv;
    @InjectView(R.id.tv_car_dealers_phone)
    TextView dealersPhoneTv;
    @InjectView(R.id.tv_dealer_address)
    TextView dealersAddressTv;
    @InjectView(R.id.tv_car_vin)
    TextView carVinTv;
    @InjectView(R.id.tv_on_the_card_time)
    TextView carTimeTv;
    @InjectView(R.id.tv_car_color)
    TextView carColorTv;
    @InjectView(R.id.tv_displacement)
    TextView carDisplacement;
    @InjectView(R.id.tv_use_nature)
    TextView carUseNature;
    @InjectView(R.id.tv_emission)
    TextView caremussinTv;
    @InjectView(R.id.tv_mileage)
    TextView carMileageTv;
    @InjectView(R.id.tv_mortgage)
    TextView carMortgageTv;
    @InjectView(R.id.tv_car300)
    TextView car300Tv;
    @InjectView(R.id.tv_reserve_price)
    TextView carReservePriceTv;
    @InjectView(R.id.tv_factory_time)
    TextView carFactoryTv;
    @InjectView(R.id.tv_transfer)
    TextView cartransferTv;
    @InjectView(R.id.tv_maintenance)
    TextView carMaintenanceTv;
    @InjectView(R.id.tv_driving_license)
    TextView drivingTv;
    @InjectView(R.id.tv_registration)
    TextView registrationTv;
    @InjectView(R.id.tv_key_num)
    TextView keyNumTv;
    @InjectView(R.id.tv_after_warranty)
    TextView afterTv;
    @InjectView(R.id.tv_warranty_expire)
    TextView warrantyTv;
    @InjectView(R.id.tv_commercial_insurance)
    TextView commercialTv;
    @InjectView(R.id.commercial_expire)
    TextView commercial_expire;
    @InjectView(R.id.tv_seat_function)
    TextView seatFunctiontV;
    @InjectView(R.id.tv_airbag)
    TextView airbagTv;
    @InjectView(R.id.tv_gearbox)
    TextView gearboxTv;
    private String carID;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_car_details, null);
        ButterKnife.inject(this, super.onCreateView(inflater, container, savedInstanceState));
        presenter = new CarDetailsPresenterImpl(this);
        presenter.getCarDetails(getActivity());
        carID = "车源ID:"+CustomSharedPreferences.getString(context, KeyBean.CARID.valueOf(),KeyBean.FILE_NAME.valueOf());
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void getCarDateils(CarDetailsBean carBean) {
        setInfo(carBean);
        List<String> imageTitle = new ArrayList<>();

        List<String> imageUrls = new ArrayList<>();
        for (int i = 0; i < carBean.getBannerImg().size(); i++) {
            String image = carBean.getBannerImg().get(i);
            Logger.d(image);
            imageTitle.add(carID);
            imageUrls.add(image);
        }
        //设置banner样式（显示圆形指示器和标题）
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        //设置banner动画效果（正方体旋转式）
        //banner.setBannerAnimation(Transformer.CubeOut);
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerTitles(imageTitle);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(4000);
        //设置指示器位置：左侧显示（当banner模式中有指示器时）
        //banner.setIndicatorGravity(BannerConfig.RIGHT);
        //新版的banner的使用----偷下懒的使用方法
        banner.setImages(imageUrls).setImageLoader(new ImageLoaderInterface() {
            @Override
            public void displayImage(Context context, Object path, View imageView) {
                Glide.with(context)
                        .load(path)
                        .placeholder(R.mipmap.ic_placeholder_small)
                        .error(R.mipmap.ic_loadding_error_small) // will be displayed if the image canot be loaded
                        .into((ImageView) imageView);
            }
            @Override
            public View createImageView(Context context) {
                return null;
            }
        }).start();
    }
    @OnClick({R.id.iv_maintain_msg,R.id.iv_maintain_phone,R.id.iv_dealers_phone,R.id.iv_dealers_msg})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_maintain_phone:
                call(carMainPhoneTv.getText().toString());
                break;
            case R.id.iv_maintain_msg:
                sendto(carMainPhoneTv.getText().toString());
                break;
            case R.id.iv_dealers_phone:
                call(dealersPhoneTv.getText().toString());
                break;
            case R.id.iv_dealers_msg:
                sendto(dealersPhoneTv.getText().toString());
                break;
        }
    }

    /**
     * 调用拨号功能
     *
     * @param phone 电话号码
     */

    private void  call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getActivity().startActivity(intent);
    }

    private void sendto(String phone) {
        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getActivity().startActivity(intent);
    }
    private void setInfo(CarDetailsBean carBean) {
        if (!TextUtils.isEmpty(carBean.getCarInfo().getTitle())) {
            titleTv.setText(carBean.getCarInfo().getTitle());
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getStatus())) {
            String status = carBean.getCarInfo().getStatus();
            if (status.equals("TRACK")){
                status = "线索";
            }
            if (status.equals("EXAMINING")){
                status = "总部待审";
            }
            if (status.equals("KENTNOPASS")){
                status = "总部不通过";
            }
            if (status.equals("PUBLISHPASS")){
                status = "上架";
            }
            if (status.equals("COMMUNICATED")){
                status = "内部交流";
            }
            if (status.equals("ENDDOWN")){
                status = "下架";
            }

            statusTv.setText("车源状态："+status);
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getOrigin())) {
            originTv.setText(carBean.getCarInfo().getOrigin());
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getPrice())) {
            priceTv.setText(carBean.getCarInfo().getPrice() + "万");
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getOriginInfo().getDealer_address())) {
            dealersAddressTv.setText(carBean.getCarInfo().getOriginInfo().getDealer_address());
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getOriginInfo().getDealer_name())) {
            dealersNameTv.setText(carBean.getCarInfo().getOriginInfo().getDealer_name());
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getOriginInfo().getDealer_boss())) {
            dealersContactTv.setText(carBean.getCarInfo().getOriginInfo().getDealer_boss());
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getOriginInfo().getDealer_phone())) {
            dealersPhoneTv.setText(carBean.getCarInfo().getOriginInfo().getDealer_phone());
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getOriginInfo().getExamines_name())) {
            carMaintainTv.setText(carBean.getCarInfo().getOriginInfo().getExamines_name());
        }
        if (!TextUtils.isEmpty(carBean.getCarInfo().getOriginInfo().getExamines_phone())) {
            carMainPhoneTv.setText(carBean.getCarInfo().getOriginInfo().getExamines_phone());
        }

    /* ##############################################基本信息############################################## **/
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getVin())) {
            carVinTv.setText(carBean.getCarDetailsInfo().getBasicInformation().getVin());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getCar_color())) {
            carColorTv.setText(carBean.getCarDetailsInfo().getBasicInformation().getCar_color());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getLicense_time())) {
            carTimeTv.setText(carBean.getCarDetailsInfo().getBasicInformation().getLicense_time());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getDisplacement())) {
            carDisplacement.setText(carBean.getCarDetailsInfo().getBasicInformation().getDisplacement() + "L");
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getEmission_standard())) {
            String carEmu = carBean.getCarDetailsInfo().getBasicInformation().getEmission_standard();
            if (carEmu.equals("1")) {
                caremussinTv.setText("国二");
            }
            if (carEmu.equals("2")) {
                caremussinTv.setText("国三");
            }
            if (carEmu.equals("3")) {
                caremussinTv.setText("国四");
            }
            if (carEmu.equals("4")) {
                caremussinTv.setText("国五");
            }
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getTravel_mileage())) {
            carMileageTv.setText(carBean.getCarDetailsInfo().getBasicInformation().getTravel_mileage() + "万公里");
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getMortgage())) {
            String mortg = carBean.getCarDetailsInfo().getBasicInformation().getMortgage();
            switch (Integer.valueOf(mortg)) {
                case 1:
                    carMortgageTv.setText("无按揭");
                    break;
                case 2:
                    carMortgageTv.setText("已还清，但未拿证");
                    break;
                case 3:
                    carMortgageTv.setText("没还清，卖方还");
                    break;
                case 4:
                    carMortgageTv.setText("没还清 买方还");
                    break;
            }
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getProduction_time())) {
            carFactoryTv.setText(carBean.getCarDetailsInfo().getBasicInformation().getProduction_time());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getTransfer_num())) {
            cartransferTv.setText(carBean.getCarDetailsInfo().getBasicInformation().getTransfer_num());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getMaintain_place())) {

            String maintainPlace = carBean.getCarDetailsInfo().getBasicInformation().getMaintain_place();
            if (maintainPlace.equals("1")) {
                carMaintenanceTv.setText("4S");
            } else {
                carMaintenanceTv.setText("非4S");
            }
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getLow_price())) {
            carReservePriceTv.setText(carBean.getCarDetailsInfo().getBasicInformation().getLow_price() + "万");
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getFair_price())) {
            car300Tv.setText(carBean.getCarDetailsInfo().getBasicInformation().getFair_price() + "万");
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getBasicInformation().getUse_property())) {
            String usePro = carBean.getCarDetailsInfo().getBasicInformation().getUse_property();
            if (usePro.equals("0")) {
                carUseNature.setText("不详");
            }
            if (usePro.equals("1")) {
                carUseNature.setText("非营运");
            }
            if (usePro.equals("2")) {
                carUseNature.setText("运营转非营运");
            }
            if (usePro.equals("3")) {
                carUseNature.setText("营运");
            }
        }
    /* ##############################################手续信息############################################## **/

        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getProceduresInformation().getIs_vehicle_passport())) {
            String driving = carBean.getCarDetailsInfo().getProceduresInformation().getIs_vehicle_passport();
            if (driving.equals("0")) {
                drivingTv.setText("不详");
            }
            if (driving.equals("1")) {
                drivingTv.setText("有");
            }
            if (driving.equals("2")) {
                drivingTv.setText("无");
            }
        }

        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getProceduresInformation().getRegistration())) {
            String regis = carBean.getCarDetailsInfo().getProceduresInformation().getRegistration();
            if (regis.equals("1")) {
                registrationTv.setText("有");
            }
            if (regis.equals("2")) {
                registrationTv.setText("无");
            }
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getProceduresInformation().getKey_num())) {
            keyNumTv.setText(carBean.getCarDetailsInfo().getProceduresInformation().getKey_num());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getProceduresInformation().getAfter_warranty())) {
            String warranty = carBean.getCarDetailsInfo().getProceduresInformation().getAfter_warranty();
            if (warranty.equals("0")) {
                afterTv.setText("不详");
            }
            if (warranty.equals("1")) {
                afterTv.setText("过保");
            }
            if (warranty.equals("2")) {
                afterTv.setText("未过保");
            }
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getProceduresInformation().getCommercial_insurance_expire())) {
            commercial_expire.setText(carBean.getCarDetailsInfo().getProceduresInformation().getCommercial_insurance_expire());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getProceduresInformation().getCompulsory_insurance_expire())) {
            warrantyTv.setText(carBean.getCarDetailsInfo().getProceduresInformation().getCompulsory_insurance_expire());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getProceduresInformation().getCommercial_insurance())) {
            String commercial = carBean.getCarDetailsInfo().getProceduresInformation().getCommercial_insurance();
            if (commercial.equals("0")) {
                commercialTv.setText("不详");
            }
            if (commercial.equals("1")) {
                commercialTv.setText("有");
            }
            if (commercial.equals("2")) {
                commercialTv.setText("无");
            }
        }

        /* ##############################################配置信息############################################## **/

        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getConfigurationInformation().getGasbug())) {
            airbagTv.setText(carBean.getCarDetailsInfo().getConfigurationInformation().getGasbug());
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getConfigurationInformation().getGearbox())) {
            String gearbox = carBean.getCarDetailsInfo().getConfigurationInformation().getGearbox();
            if (gearbox.equals("1")) {
                gearboxTv.setText("自动");
            }
            if (gearbox.equals("2")) {
                gearboxTv.setText("手动");
            }
        }
        if (!TextUtils.isEmpty(carBean.getCarDetailsInfo().getConfigurationInformation().getSeat_function())) {
            String function = carBean.getCarDetailsInfo().getConfigurationInformation().getSeat_function();
            if (function.equals("0")) {
                seatFunctiontV.setText("无");
            }
            if (function.equals("1")) {
                seatFunctiontV.setText("电加热");
            }
            if (function.equals("2")) {
                seatFunctiontV.setText("通风");
            }
            if (function.equals("3")) {
                seatFunctiontV.setText("按摩");
            }

        }
    }
}
